<template>
  <div>
    <el-dialog
      title="新增"
      :visible.sync="modelDialogVisible"
      width="500px"
      :before-close="closeEditDialog"
      :close-on-click-modal="false"
      :append-to-body="true"
    >
      <el-form
        :model="addInfo"
        :rules="rules"
        ref="refAddInfo"
        label-width="110px"
      >
        <el-form-item size="small" label="型号" prop="model">
          <el-radio-group v-model="addInfo.model" @change="onChange">
            <el-radio label="default">默认</el-radio>
            <el-radio label="custom">自定义</el-radio>
          </el-radio-group>
          <el-input
            v-if="addInfo.model == 'custom'"
            v-model.trim="modelName"
            placeholder="请输入型号"
            maxlength="30"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item
          size="small"
          label="型号名称"
          prop="name"
          v-if="addInfo.model == 'custom'"
        >
          <el-input
            v-model.trim="addInfo.name"
            placeholder="请输入型号名称"
            maxlength="30"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item size="small" label="状态" prop="model">
          <el-radio-group v-model="addInfo.state">
            <el-radio label="dev">开发</el-radio>
            <el-radio label="publish">发布</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item size="small" label="脚本" prop="type">
          <el-radio-group v-model="addInfo.type">
            <el-radio label="JavaScript">JavaScript</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeEditDialog" class="cancel-button">取 消</el-button>
        <el-button
          size="small"
          @click="onSubmit"
          v-preventReClick="1000" class="confirm-button"
          >确 定</el-button
        >
      </span>
      <codemirror v-model="addInfo.script" :options="options"></codemirror>
    </el-dialog>
  </div>
</template>

<script>
import { codemirror } from "vue-codemirror";
import "codemirror/lib/codemirror.css";
import "codemirror/theme/3024-day.css";
import "codemirror/theme/ayu-mirage.css";
import "codemirror/theme/monokai.css";
import "codemirror/theme/rubyblue.css";
import "codemirror/mode/htmlmixed/htmlmixed.js";
import "codemirror/mode/javascript/javascript.js";

import { reqModelAdd, reqModelEdit } from "@api/model";

export default {
  name: "",
  props: ["type", "modelInfo", "productKey"],
  components: {
    codemirror,
  },
  data() {
    return {
      modelDialogVisible: false,
      addInfo: {
        name: "默认",
        model: "default",
        state: "dev",
        type: "JavaScript",
        productKey: "",
        script: `
//编码函数
this.encode(service){
  return "";
}
//解码函数
this.decode(msg){
  return {};
}
        `,
      },
      modelName: "_default",

      // code
      options: {
        tabSize: 4,
        mode: "text/javascript",
        // theme: "base16-light",
        theme: "monokai",
        lineNumbers: true,
        line: true,
      },

      // 表单验证规则
      rules: {
        model: [{ required: true, message: "请选择型号", trigger: "change" }],
        type: [{ required: true, message: "请选择脚本", trigger: "type" }],
      },
    };
  },
  watch: {
    modelDialogVisible(val) {
      // if (val && JSON.stringify(this.modelInfo) != "{}") {
      console.log(this.modelInfo, "this.type");
      if (val) {
        switch (this.type) {
          case "add":
            this.addInfo = {
              name: "默认",
              model: "default",
              state: "dev",
              type: "JavaScript",
              productKey: "",
              script: `
//编码函数
this.encode(service){
  return "";
}
//解码函数
this.decode(msg){
  return {};
}
        `,
            };
            this.modelName = "_default";
            break;
          case "edit":
            this.addInfo.name = this.modelInfo.name;
            this.addInfo.state = this.modelInfo.state;
            this.addInfo.type = this.modelInfo.type;
            this.addInfo.productKey = this.modelInfo.productKey;
            this.addInfo.script = this.modelInfo.script;
            if (this.modelInfo.model != "default") {
              this.modelName = this.modelInfo.model;
              this.addInfo.model = "custom";
            }
        }
      }
    },
  },
  methods: {
    onChange(e) {
      console.log(e, "E");
    },
    // 点击
    async onSubmit() {
      this.$refs.refAddInfo.validate(async (valid) => {
        if (valid) {
          this.addInfo.productKey = this.productKey;
          if (this.addInfo.model != "default") {
            this.addInfo.model = this.modelName;
          }
          let res;
          if (this.type == "add") {
            res = await reqModelAdd({
              data: this.addInfo,
            });
          } else if (this.type == "edit") {
            this.addInfo.id = this.modelInfo.id;
            res = await reqModelEdit({
              data: this.addInfo,
            });
          }
          if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
          this.$modal.notifySuccess("操作成功");
          this.$parent.onModels();
          this.closeEditDialog();
        }
      });
    },
    // 点击关闭弹框
    closeEditDialog() {
      this.uploadDisabled = false;
      this.$refs.refAddInfo.resetFields();
      this.modelDialogVisible = false;
    },
  },
};
</script>

<style lang="less" scoped>
@import url("../../../../../common.less");
</style>
